import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.s10.model.Dashboard;
import com.s10.model.UserCredentials;

public class LoginDashboard {

	JFrame frame;
	JPanel panel, panel2;
	JLabel l1, l2;
	JTextField t1, t2;
	JButton loginButton;
	final String UNSUCCESSFUL_MESSAGE = "Unsuccessful login. Try again";

	public LoginDashboard() {

		frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(300, 130);

		panel = new JPanel();
		frame.add(panel);

		l1 = new JLabel();
		l2 = new JLabel();

		l1.setText("Please enter username:");
		l2.setText("Please enter password:");

		t1 = new JTextField();
		t1.setPreferredSize(new Dimension(100, 20));

		t2 = new JTextField();
		t2.setPreferredSize(new Dimension(100, 20));

		panel.add(l1);
		panel.add(t1);

		panel.add(l2);
		panel.add(t2);

		loginButton = new JButton("Login");
		panel.add(loginButton);

		loginButton.addActionListener(new ActionListener() {

			UserCredentials userCredentials = new UserCredentials();

			@Override
			public void actionPerformed(ActionEvent e) {
				userCredentials.setPassword(t2.getText());
				userCredentials.setUsername(t1.getText());
				if (userCredentials.verifyCredentials()) {
					frame.dispose();
					new Dashboard(userCredentials.getUsername());
				} else {
					t1.setText("");
					t2.setText("");
					JOptionPane.showMessageDialog(frame,
							UNSUCCESSFUL_MESSAGE);
				}
			}
		});

		frame.setVisible(true);

	}

	public static void main(String[] args) {
		new LoginDashboard();

	}

}
